﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInformation;

namespace WebsiteAdmin.Controllers
{
    [Authorize]
    public class LeaveMsgTypeController : Controller
    {
        public ActionResult GetAllMsgType()
        {
            WI_LeaveMsgTypeDAL mtdal = new WI_LeaveMsgTypeDAL();
            List<WI_LeaveMsgType> wimtlist = mtdal.GetAllMsgType();
            List<LeaveMsgType> mtlist = new List<LeaveMsgType>();
            mtlist.Add(new LeaveMsgType { TypeID = 0, MsgType = "全部" });
            foreach (var wimtype in wimtlist)
            {
                LeaveMsgType msgtype = new LeaveMsgType
                {
                    TypeID = wimtype.TypeID,
                    MsgType = wimtype.MsgType
                };
                mtlist.Add(msgtype);
            }
            return this.Store(mtlist);
        }

        [HttpPost]
        public ActionResult DeleteMsgType(List<LeaveMsgType> delMsgType)
        {
            try
            {
                WI_LeaveMsgTypeDAL mtdal = new WI_LeaveMsgTypeDAL();
                foreach (var mtype in delMsgType)
                {
                    mtdal.RemoveMsgType(mtype.TypeID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewMsgTypeWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建留言类别",
                Width = 500,
                Height = 150,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateMsgType"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateMsgType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateMsgType(string MsgType)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_LeaveMsgTypeDAL mtdal = new WI_LeaveMsgTypeDAL();
                mtdal.CreateMsgType(MsgType);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "类别创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        [HttpPost]
        public ActionResult UpdateMsgType(List<LeaveMsgType> updMtypes)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_LeaveMsgTypeDAL mtdal = new WI_LeaveMsgTypeDAL();
                foreach (var mtype in updMtypes)
                {
                    mtdal.UpdateMsgType(mtype.TypeID, mtype.MsgType);
                }
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "类别更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
